<?php

namespace Modules\Admin\Http\Controllers\WorkMessage;

use Modules\Common\Models\WorkMessage;
use Modules\Common\Util\Api;
use Modules\Common\Util\Time;


class Retrieve
{


    /**
     * @Route(path=/api/admin/work-message)
     */
    public function retrieve()
    {
        $query = WorkMessage::query()
            ->select(['id', 'branch_id', 'enterprise_id', 'title', 'status', 'note', 'created_at']);

        if (request('enterprise_id')) {
            $query->where('enterprise_id', request('enterprise_id'));
        }

        if (request('status')) {
            $query->where('status', request('status'));
        }
        if (request('start') && request('end')) {
            $start = Time::format(request('start'));
            $end = Time::format(date('Y-m-d H:i:s', strtotime(request('end')) + 86399));
            $query->whereBetween('created_at', [$start, $end]);
        }

        $pagenate = $query->orderBy('status', 'asc')
            ->orderBy('created_at', 'desc')
            ->paginate(request('per_page', 15));


        $unHandledCount = WorkMessage::where('status', 1)->count();

        return Api::success($pagenate, $unHandledCount);

    }


}
